package com.demon.androidbasic.base

import android.os.Bundle
import androidx.viewbinding.ViewBinding
import com.demon.androidbasic.interfaces.LifeCycleListener


abstract class BaseLifecycleActivity<T : ViewBinding> : MyActivity<T>() {
    protected var mTag: String? = null
    protected var mLifeCycleListeners: MutableList<LifeCycleListener>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mTag = this.javaClass.simpleName
        mLifeCycleListeners = mutableListOf()
        if (mLifeCycleListeners != null) {
            for (listener in mLifeCycleListeners!!) {
                listener.onCreate()
            }
        }
    }


    override fun onDestroy() {
        if (mLifeCycleListeners != null) {
            for (listener in mLifeCycleListeners!!) {
                listener.onDestroy()
            }
            mLifeCycleListeners!!.clear()
            mLifeCycleListeners = null
        }
        super.onDestroy()
    }

    override fun onStart() {
        super.onStart()
        if (mLifeCycleListeners != null) {
            for (listener in mLifeCycleListeners!!) {
                listener.onStart()
            }
        }
    }

    override fun onResume() {
        super.onResume()
        if (mLifeCycleListeners != null) {
            for (listener in mLifeCycleListeners!!) {
                listener.onResume()
            }
        }
    }

    override fun onPause() {
        super.onPause()
        if (mLifeCycleListeners != null) {
            for (listener in mLifeCycleListeners!!) {
                listener.onPause()
            }
        }
    }

    override fun onStop() {
        super.onStop()
        if (mLifeCycleListeners != null) {
            for (listener in mLifeCycleListeners!!) {
                listener.onStop()
            }
        }
    }

    open fun addLifeCycleListener(listener: LifeCycleListener?) {
        if (mLifeCycleListeners != null && listener != null) {
            mLifeCycleListeners!!.add(listener)
        }
    }

    open fun addAllLifeCycleListener(listeners: List<LifeCycleListener>?) {
        if (mLifeCycleListeners != null && listeners != null) {
            mLifeCycleListeners!!.addAll(listeners)
        }
    }

    open fun removeLifeCycleListener(listener: LifeCycleListener) {
        if (mLifeCycleListeners != null) {
            mLifeCycleListeners!!.remove(listener)
        }
    }

}